package org.HisCanvas.ChrisRiffle.JFlightLog.swingUi.table;

import java.awt.Dimension;
import java.util.List;

import javax.swing.JScrollPane;
import javax.swing.JTable;

import org.HisCanvas.ChrisRiffle.JFlightLog.common.LogBookRecUtils;
import org.HisCanvas.ChrisRiffle.JFlightLog.dataModel.LogBookRec;
import org.HisCanvas.ChrisRiffle.JFlightLog.launching.JFlightLogController;
import org.HisCanvas.ChrisRiffle.JFlightLog.logging.LogController;


/**
 * FlightLogTableController
 * @author rif
 *
 */
public class FlightLogTableController {

	
	private static FlightLogTableController INSTANCE;
	
	private LogBookTable logBookTable;
	
	private JScrollPane logBookTableScrollPane;
	
	
	/**
	 * private singleton enforcement
	 */
	private FlightLogTableController() {
	}
	
	/**
	 * Singleton getter
	 * 
	 * @return
	 * 		singleton
	 */
	public static FlightLogTableController instance(){
		if(INSTANCE == null){
			INSTANCE = new FlightLogTableController();
		}
		return INSTANCE;		
	}
	
	
	
	/**
	 * getLogBookTableScrollPane
	 * @return
	 */
	public JScrollPane getLogBookTableScrollPane(){
		
		if(logBookTableScrollPane == null){
			
			logBookTableScrollPane = new JScrollPane(getLogBookTable());	
			
			logBookTableScrollPane .setPreferredSize(new Dimension(1000,250));
		}
		return logBookTableScrollPane;		
	}
	
	
	
	/**
	 * getLogBookTable
	 * @return
	 */
	public LogBookTable getLogBookTable(){
		
		if(logBookTable == null){
			
			logBookTable = new LogBookTable();	
			
																		
			//simple param so just construct it here to add it
			logBookTable.addFocusListener(new FlightTableFocusListener());
			
			//simple param object so just create it here when adding it
			logBookTable.addMouseListener(new FlightLogTableMouseListener());
			
			new FlightLogTableModelSetterWorker(logBookTable, 
					LogBookRecUtils.DEFAULT_COLUMN_DISPLAY_ORDER_LIST).execute();
									
		}
		return logBookTable;
	}
	

	
	
	public void refreshTableView(){
		
		JTable table = getLogBookTable();
		
		List<LogBookRec> recList = null;
		
		try {
			
			recList = JFlightLogController.instance().getLogBookDao().getLogBook().getLogBookRecList();
			
			((FlightLogTableModel)table.getModel()).refreshTableView(recList);
			
		} catch (Exception e) {
			
			LogController.severe("Couldn't retrieve log book rec list, so can't refresh table");
			e.printStackTrace();
		}
		
				
	}
	
	
	/**
	 * Returns the selected flight - or null if nothing is 
	 * selected
	 */
	public LogBookRec getSelectedFlight(){
		
		if(!isRowSelected()){
			return null;
		}
		
		int selectedRowIndex = getLogBookTable().getSelectedRow();
		
		FlightLogTableModel tableModel = (FlightLogTableModel)getLogBookTable().getModel();
		
		LogBookRec rSelectedRec = tableModel.getLogBookRec(selectedRowIndex);
		
		return rSelectedRec;
	}
	
	
	/**
	 * Returns true if a flight log table rec is selected
	 * 
	 * @return
	 * 		true if a flight log table rec is selected
	 */
	public boolean isRowSelected(){
	
		return getLogBookTable().getSelectedRowCount() > 0;
	}
	
		
}



